﻿@using System.Collections
@using HouseHoldBudget.App_GlobalResources
@using HouseHoldBudget.Models.Budgets
@using HouseHoldBudget.Models.User
@using Kendo.Mvc.UI
@model BudgetModel
@Html.ValidationSummary(false)
@Html.HiddenFor(model => model.Id)

@Html.Kendo().TabStrip().Animation(false).Name("budget-tab").Items(x =>
{
    var tabInfo = x.Add().Text("Général").Content(TabInfo().ToHtmlString()).Selected(true);

    Kendo.Mvc.UI.Fluent.TabStripItemBuilder tabActors = null;
    if (Model.IsEditMode)
        tabActors = x.Add().Text("Acteurs").Content(TabActors().ToHtmlString());

    Kendo.Mvc.UI.Fluent.TabStripItemBuilder tabCategories = null;
    if (Model.IsEditMode)
        tabCategories = x.Add().Text("Catégories").Content(TabCategories().ToHtmlString());

    Kendo.Mvc.UI.Fluent.TabStripItemBuilder tabCategoriesExpenseTypes = null;
    if (Model.IsEditMode)
        tabCategoriesExpenseTypes = x.Add().Text("Catégories - Types de dépenses").Content(TabManageCategoriesExpenseTypes().ToHtmlString());
})

@helper TabInfo()
    {
    <table class="content">
        <tr>
            <td class="adminTitle">
                @Html.LabelFor(model => model.BudgetName):
            </td>
            <td class="adminData">
                @Html.EditorFor(model => model.BudgetName)
                @Html.ValidationMessageFor(model => model.BudgetName)
            </td>
        </tr>
        <tr>
            <td class="adminTitle">
                @Html.LabelFor(model => model.BudgetDescription):
            </td>
            <td class="adminData">
                @Html.EditorFor(model => model.BudgetDescription)
            </td>
        </tr>
        <p></p>
        <tr>
            <td class="adminTitle">
                @Html.LabelFor(model => model.BudgetTypesItems):
            </td>
            <td class="adminData">
                @Html.DropDownListFor(model => model.BudgetTypeId, Model.BudgetTypesItems, new { @class = "k - dropdown" })
            </td>
        </tr>
        <tr>
            <td class="adminTitle">
                <label for="UserFullName">
                    Propriétaire:</label>
            </td>
            <td class="adminData">
                @Html.DisplayFor(model => model.BudgetOwner.UserFullName)
            </td>
        </tr>
    </table>
        
    <table class="content">
        <tr>
            <td class="adminTitle">
                <label for="UserFullName">
                    Total de mes revenus:</label>
            </td>
            <td class="adminData">
                @Html.DisplayFor(model => model.BudgetOwner.TotalFixedIncomesAmount)
            </td>
        </tr>
    </table>
}
@helper TabActors()
    {
        @(Html.Kendo().Grid<BudgetUserModel>()
            .Name("grid-budget-actors")
            .Columns(columns =>
            {
                columns.Command(commands =>
                {
                    commands.Edit();
                    commands.Destroy();
                }).Width(200);
                columns.ForeignKey(o => o.UserId, (IEnumerable) ViewData["allUsers"], "UserId", "UserFullName").Title(@CustomResources.Common_Users);
                columns.Bound(x => x.TotalIncomesAmount)
                    .Title(@CustomResources.Budget_CreateEdit_TotalIncomes)
                    .Format("{0:C}")
                    .ClientFooterTemplate("Total: #= kendo.toString(sum, 'C') #")
                    .ClientGroupFooterTemplate("Total: #= kendo.toString(sum, 'C') #");                    
            })
            .Events(evt => evt.DataBound("grid_budget_actors_onDataBound"))
            .ToolBar(toolbar => toolbar.Create().Text(@CustomResources.Common_Add))
            .Editable(editable => editable.Mode(Kendo.Mvc.UI.GridEditMode.InLine))
            .DataSource(dataBinding => dataBinding.Ajax()
                                        .Model(model =>
                                        {
                                            model.Id(p=>p.Id);
                                            model.Field(p => p.Id).DefaultValue(Model.Id);
                                            model.Field(f => f.BudgetId).DefaultValue(Model.Id);
                                            model.Field(p => p.UserId).DefaultValue(ViewData["defaultUserId"]);
                                        })
                                .Read(read => read.Action("SelectBudgetActors", "Budget", new { budgetId = Model.Id }))
                                .Create(create => create.Action("InsertBudgetActor", "Budget"))
                                .Update(update => update.Action("UpdateBudgetActor", "Budget"))
                                .Destroy(destroy => destroy.Action("DeleteBudgetActor", "Budget", new { budgetId = Model.Id }))
                                .Aggregates(agg => agg.Add(o => o.TotalIncomesAmount).Sum())
                                .PageSize(20))
            .Scrollable()
            .Sortable()
            .Filterable()) 
}
@helper TabCategories()
    {
        @(Html.Kendo().Grid<BudgetCategoryModel>()
            .Name("grid-budget-categories")
            .Columns(columns =>
            {
                columns.Command(commands =>
                {
                    commands.Edit();
                    commands.Destroy();
                }).Width(250);
                columns.Bound(b => b.BudgetCategoryName).Title(@CustomResources.Budget_CreateEdit_CategoryName);
                columns.Bound(b => b.RepartitionAmount).Title(@CustomResources.Budget_CreateEdit_Amount).Format("{0:C}").ClientFooterTemplate("Total: #= kendo.toString(sum, 'C') #").ClientGroupFooterTemplate("Total: #= kendo.toString(sum, 'C') #");
            })
            .ToolBar(toolbar => toolbar.Create().Text(@CustomResources.Common_Add))
            .Editable(editable => editable.Mode(Kendo.Mvc.UI.GridEditMode.InLine))
            .DataSource(dataBinding => dataBinding
                                        .Ajax()
                                        .Events(evt => evt.Sync("grid_budget_categories_expensetypes_callback"))
                                        .Model(model =>
                                        {
                                            model.Id(p => p.Id);
                                            model.Field(p => p.Id).DefaultValue(Model.Id);
                                            model.Field(f => f.BudgetId).DefaultValue(Model.Id);
                                        })
                                        .Read(read => read.Action("SelectBudgetCategories", "Budget", new { budgetId = Model.Id }))
                                        .Create(create => create.Action("InsertBudgetCategory", "Budget"))
                                        .Update(update => update.Action("UpdateBudgetCategory", "Budget"))
                                        .Destroy(destroy => destroy.Action("DeleteBudgetCategory", "Budget", new { budgetId = Model.Id }))
                                        .Aggregates(agg => agg.Add(o => o.RepartitionAmount).Sum())
                                        .PageSize(20))
            .Scrollable()
            .Sortable()
            .Filterable())         
        
}
@helper TabManageCategoriesExpenseTypes()
    {
        @(Html.Kendo().Grid<BudgetCategoryExpenseTypesModel>()
            .Name("grid-budget-categories-expensetypes")
            .Columns(columns =>
            {
                columns.Command(commands =>
                {
                    commands.Edit();
                    commands.Destroy();
                }).Width(200);
                columns.Bound(b => b.ExpenseTypeName).Title(@CustomResources.Expenses_ManageExpenseTypesTitle);
                columns.Bound(b => b.BudgetCategory).Title(@CustomResources.Budget_CreateEdit_AttachedCategory).ClientTemplate("#=BudgetCategory.BudgetCategoryName#");
            })
            .Events(evt => evt.DataBound("grid_budget_categories_expensetypes_onDataBound").Edit("removeDelete"))
            .Editable(editable => editable.Mode(Kendo.Mvc.UI.GridEditMode.InLine))
            .DataSource(dataBinding => dataBinding
                                        .Ajax()
                                        .Model(model =>
                                        {
                                            model.Id(p => p.Id);
                                            model.Field(p => p.Id).DefaultValue(Model.Id);
                                            model.Field(f => f.BudgetId).DefaultValue(Model.Id);
                                            model.Field(f => f.ExpenseTypeId);
                                            model.Field(f => f.ExpenseTypeName).Editable(false);
                                            model.Field(f => f.BudgetCategory);
                                        })
                                        .Read(read => read.Action("SelectBudgetCategoryExpenseTypes", "Budget", new { budgetId = Model.Id }))
                                        .Update(update => update.Action("UpdateBudgetCategoryExpenseTypes", "Budget"))
                                        .Destroy(destroy => destroy.Action("DeleteBudgetCategoryExpenseTypes", "Budget", new { budgetId = Model.Id }))
                                        .PageSize(20))
            .Scrollable()
            .Sortable()
            .Filterable())           
}

<script type="text/javascript">
   function grid_budget_categories_expensetypes_onDataBound(e) {
        //Selects all delete buttons
        $("#grid-budget-categories-expensetypes tbody tr .k-grid-delete").each(function () {
           $(this).remove();
        });
   }
   function grid_budget_actors_onDataBound(e) {
        //Selects all edit buttons
        $("#grid-budget-actors tbody tr .k-grid-edit").each(function () {
            $(this).remove();
        });
   }
   function onBudgetCategoryAdditionalData() {
        //Used by category list editor
        return {
            budgetId: $("#Id").val()
        };
   }
   function removeDelete(e) {
       var idx = e.container.index(),
        parent = e.container.parent();
       e.container.find(".k-grid-cancel").click(function () {
           window.setTimeout(function () {
               parent.children().eq(idx).find('a.k-grid-delete').remove();
           }, 1);
       });
   }
   function grid_budget_categories_expensetypes_callback(e) {
       var grid = $('#grid-budget-categories-expensetypes').data('kendoGrid');
       grid.dataSource.read();
   }
</script>
